package org.nanhill.support.jmx.dwjmxservice;

import javax.management.Attribute;
import javax.management.AttributeList;
import javax.management.DynamicMBean;
import javax.management.MBeanAttributeInfo;
import javax.management.MBeanConstructorInfo;
import javax.management.MBeanInfo;
import javax.management.MBeanOperationInfo;
import javax.swing.UnsupportedLookAndFeelException;

public class ClickMeterDyn extends ClickMeterInstrm implements DynamicMBean {

	public ClickMeterDyn() {
	}

	public static void main(String[] args) throws ClassNotFoundException,
			InstantiationException, IllegalAccessException,
			UnsupportedLookAndFeelException {
		setLandF();
		ClickMeterDyn cms = new ClickMeterDyn();
		// can use standard MBean agent - same logic
		BaseStdAgent myAgent = new BaseStdAgent();
		myAgent.startAgent((Object) cms);

	}

	// implements the dynamic MBean interface

	public Object getAttribute(String inAttrName) {
		if (inAttrName.equals("PanelValue"))
			return ((Object) getPanelValue());
		else
			return null;
	}

	public void setAttribute(Attribute attribute) {
		if (attribute.getName().equals("PanelValue"))
			setPanelValue((Integer) attribute.getValue());
	}

	public AttributeList getAttributes(String[] attributeNames) {
		AttributeList resultList = new AttributeList();

		for (int i = 0; i < attributeNames.length; i++) {
			try {

				resultList.add(new Attribute(attributeNames[i],
						(Object) getAttribute((String) attributeNames[i])));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return (resultList);

	}

	public AttributeList setAttributes(AttributeList attributes) {
		AttributeList resultList = new AttributeList();

		for (int i = 0; i < attributes.size(); i++) {
			Attribute attr = (Attribute) attributes.get(i);
			try {
				setAttribute(attr);
				String name = attr.getName();

				resultList
						.add(new Attribute(name, (Object) getAttribute(name)));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return (resultList);
	}

	public Object invoke(String opName, Object params[], String signature[]) {

		if (opName.equals("incPanelValue")) {
			incPanelValue();

		} else {

			if (opName.equals("decPanelValue")) {
				decPanelValue();
			}
		}
		return null;
	}

	public MBeanInfo getMBeanInfo() {
		MBeanAttributeInfo[] mbai = new MBeanAttributeInfo[1];
		mbai[0] = new MBeanAttributeInfo("PanelValue", "java.lang.Integer",
				"The Panel Value", true, true, false);

		MBeanConstructorInfo[] mbci = new MBeanConstructorInfo[1];
		mbci[0] = new MBeanConstructorInfo("ClickMeterDyn",
				"Dynamic MBean Click Meter", null);

		MBeanOperationInfo[] mboi = new MBeanOperationInfo[2];
		mboi[0] = new MBeanOperationInfo("incPanelValue",
				"increment the meter value", null, "void",
				MBeanOperationInfo.ACTION);
		mboi[1] = new MBeanOperationInfo("decPanelValue",
				"decrement the meter value", null, "void",
				MBeanOperationInfo.ACTION);
		return new MBeanInfo("dwjmxservice.basic.ClickMeterDyn",
				"MBean:Click Meter", mbai, mbci, mboi, null);
	}

}
